package com.micro.school.order.service.impl;

import java.math.BigDecimal;

import com.micro.school.order.domain.PayStudentFeePaymentDetail;
import com.micro.school.order.entity.PayStudentFeeItem;
import com.micro.school.order.entity.PayStudentPrepayment;
import com.micro.school.order.mapper.*;
import com.micro.school.order.vo.PayStudentFeeItemVo;
import com.micro.school.order.vo.PayStudentFeePaymentDetailExcelVo;
import com.micro.school.order.vo.PayStudentFeePaymentPreVo;

import java.util.*;

import com.micro.school.common.core.context.SecurityContextHolder;
import com.micro.school.order.entity.PayPrepaymentProject;
import com.micro.school.order.entity.PayStudentFee;
import com.micro.school.order.vo.PayStudentFeeVo;
import com.micro.school.system.api.domain.vo.PayStudentFeePaymentVos;
import com.micro.school.system.api.domain.vo.StudentPayVos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.micro.school.order.domain.PayStudentFeePayment;
import com.micro.school.order.service.IPayStudentFeePaymentService;

/**
 * 支付单Service业务层处理
 * 
 * @author lyb
 * @date 2025-08-19
 */
@Service
public class PayStudentFeePaymentServiceImpl implements IPayStudentFeePaymentService 
{
    @Autowired
    private PayStudentFeePaymentMapper payStudentFeePaymentMapper;

    @Autowired
    private PayStudentFeeMapper payStudentFeeMapper;


    @Autowired
    private PayPrepaymentProjectMapper payPrepaymentProjectMapper;

    @Autowired
    private PayStudentFeePaymentDetailMapper payStudentFeePaymentDetailMapper;
    @Autowired
    private PayStudentFeeItemMapper payStudentFeeItemMapper;
    @Autowired
    private PayStudentPrepaymentMapper payStudentPrepaymentMapper;

    /**
     * 查询支付单
     * 
     * @param id 支付单主键
     * @return 支付单
     */
    @Override
    public PayStudentFeePayment selectPayStudentFeePaymentById(String id)
    {
        return payStudentFeePaymentMapper.selectPayStudentFeePaymentById(id);
    }

    /**
     * 查询支付单列表
     * 
     * @param payStudentFeePayment 支付单
     * @return 支付单
     */
    @Override
    public List<PayStudentFeePayment> selectPayStudentFeePaymentList(PayStudentFeePayment payStudentFeePayment)
    {
        List<PayStudentFeePayment> payStudentFeePaymentList = payStudentFeePaymentMapper.selectPayStudentFeePaymentList(payStudentFeePayment);
        if (SecurityContextHolder.getUserIdcardState() != null){
            if (1 == SecurityContextHolder.getUserIdcardState()){
                payStudentFeePaymentList.forEach(vo -> {
                    if (vo.getIdNumber() != null && vo.getIdNumber().length() > 8) {
                        String maskedIdNumber = vo.getIdNumber().substring(0, vo.getIdNumber().length() - 8) + "********";
                        vo.setIdNumber(maskedIdNumber);
                    } else if (vo.getIdNumber() != null) {
                        vo.setIdNumber(vo.getIdNumber().replaceAll(".", "*"));
                    }
                });
            }
        }
        return payStudentFeePaymentList;
    }

    /**
     * 新增支付单
     * 
     * @param payStudentFeePayment 支付单
     * @return 结果
     */
    @Override
    public int insertPayStudentFeePayment(PayStudentFeePayment payStudentFeePayment)
    {
        return payStudentFeePaymentMapper.insertPayStudentFeePayment(payStudentFeePayment);
    }

    /**
     * 修改支付单
     * 
     * @param payStudentFeePayment 支付单
     * @return 结果
     */
    @Override
    public int updatePayStudentFeePayment(PayStudentFeePayment payStudentFeePayment)
    {
        return payStudentFeePaymentMapper.updatePayStudentFeePayment(payStudentFeePayment);
    }

    /**
     * 批量删除支付单
     * 
     * @param ids 需要删除的支付单主键
     * @return 结果
     */
    @Override
    public int deletePayStudentFeePaymentByIds(String[] ids)
    {
        return payStudentFeePaymentMapper.deletePayStudentFeePaymentByIds(ids);
    }

    /**
     * 删除支付单信息
     * 
     * @param id 支付单主键
     * @return 结果
     */
    @Override
    public int deletePayStudentFeePaymentById(String id)
    {
        return payStudentFeePaymentMapper.deletePayStudentFeePaymentById(id);
    }


    /**
     * 预缴费明细查询
     */
    @Override
    public List<PayStudentFeePaymentPreVo> selectPrepaymenttList(PayStudentFeePayment payStudentFeePayment){
        List<PayStudentFeePaymentPreVo> payStudentFeePaymentPreVos = payStudentFeePaymentMapper.selectPrepaymenttList(payStudentFeePayment);
        if (SecurityContextHolder.getUserIdcardState() != null){
            if (1 == SecurityContextHolder.getUserIdcardState()){
                payStudentFeePaymentPreVos.forEach(vo -> {
                    if (vo.getIdNumber() != null && vo.getIdNumber().length() > 8) {
                        String maskedIdNumber = vo.getIdNumber().substring(0, vo.getIdNumber().length() - 8) + "********";
                        vo.setIdNumber(maskedIdNumber);
                    } else if (vo.getIdNumber() != null) {
                        vo.setIdNumber(vo.getIdNumber().replaceAll(".", "*"));
                    }
                });
            }
        }
        return payStudentFeePaymentPreVos;
    }


    /**
     * 查询总金额-预付
     */
    @Override
    public BigDecimal selectPreSumTotalAmount(PayStudentFeePayment payStudentFeePayment){
        return payStudentFeePaymentMapper.selectPreSumTotalAmount(payStudentFeePayment);
    }

    /**
     * 查询总金额
     */
    @Override
    public BigDecimal selectTotalAmount(PayStudentFeePayment payStudentFeePayment){
        return payStudentFeePaymentMapper.selectTotalAmount(payStudentFeePayment);
    }

    /**
     * 导出明细用
     */
    @Override
    public List<PayStudentFeePaymentDetailExcelVo> selectPaymentList(PayStudentFeePayment payStudentFeePayment){
        List<PayStudentFeePaymentDetailExcelVo> payStudentFeePaymentDetailExcelVos = payStudentFeePaymentMapper.selectPaymentList(payStudentFeePayment);
        if (SecurityContextHolder.getUserIdcardState() != null){
            if (1 == SecurityContextHolder.getUserIdcardState()){
                payStudentFeePaymentDetailExcelVos.forEach(vo -> {
                    if (vo.getIdNumber() != null && vo.getIdNumber().length() > 8) {
                        String maskedIdNumber = vo.getIdNumber().substring(0, vo.getIdNumber().length() - 8) + "********";
                        vo.setIdNumber(maskedIdNumber);
                    } else if (vo.getIdNumber() != null) {
                        vo.setIdNumber(vo.getIdNumber().replaceAll(".", "*"));
                    }
                });
            }
        }
        return payStudentFeePaymentDetailExcelVos;
    }
    @Override
    public void insertOneVos(PayStudentFeePaymentVos payStudentFeePaymentVos) {
        payStudentFeePaymentMapper.insertOneVos(payStudentFeePaymentVos);
        PayStudentFeeVo payStudentFeeVo = payStudentFeeMapper.selectPayStudentFeeById(payStudentFeePaymentVos.getStudentFeeId());
        //要求需要和原账单同步更新
        PayStudentFee payStudentFee = new PayStudentFee();
        payStudentFee.setId(Math.toIntExact(payStudentFeePaymentVos.getStudentFeeId()));
        //累计支付金额 = 累计支付金额 + 实际支付金额
        payStudentFee.setAmountPaid(payStudentFeePaymentVos.getPaidAmount().doubleValue()+payStudentFeeVo.getAmountPaid());


        //代缴剩余金额 = 总金额 - 预缴抵扣金额 - 实际支付金额
        BigDecimal subtract = BigDecimal.valueOf(payStudentFeeVo.getAmountPending())
                .subtract(new BigDecimal(String.valueOf(payStudentFeePaymentVos.getPrepaymentDeducted())))
                .subtract(payStudentFeePaymentVos.getActualAmount());

        payStudentFee.setAmountPending(subtract.doubleValue());

        payStudentFee.setAmountDeducted(payStudentFeePaymentVos.getPrepaymentDeducted().doubleValue()+payStudentFeeVo.getAmountDeducted());
        if (subtract.compareTo(BigDecimal.ZERO) > 0){
            payStudentFee.setBillStatus(20);
        } else if (subtract.compareTo(BigDecimal.ZERO) == 0) {
            payStudentFee.setBillStatus(30);
        }
        payStudentFee.setSubjectId(Integer.valueOf(payStudentFeePaymentVos.getUsedPrepaymentId()));
        payStudentFee.setUpdateTime(new Date());
        PayStudentPrepayment payStudentPrepayment = payStudentPrepaymentMapper.queryById(Integer.valueOf(payStudentFeePaymentVos.getUsedPrepaymentId()));
        PayPrepaymentProject payPrepaymentProject = payPrepaymentProjectMapper.queryById(payStudentPrepayment.getSubjectId());
        payStudentFee.setProjectName(payPrepaymentProject.getProjectName());
        payStudentFee.setLastPaymentTime(new Date());
        payStudentFeeMapper.updatePayStudentFee(payStudentFee);
    }

    @Override
    public List<StudentPayVos> selectStudentFeeByStudentId(Long studentId,Long feeYearId) {
        //判断此学生有没有对应学年的缴费记录
        List<PayStudentFeePayment>  payStudentFeePaymentList = payStudentFeePaymentMapper.selectStudentFeePaymentByStudentId(studentId,feeYearId);
        List<StudentPayVos> studentPayVosList = new ArrayList<>();
        if (payStudentFeePaymentList != null && payStudentFeePaymentList.size() > 0) {
            for (PayStudentFeePayment payStudentFeePayment : payStudentFeePaymentList) {
                List<PayStudentFeePaymentDetail> payStudentFeePaymentDetailList = payStudentFeePaymentDetailMapper.selectPayStudentFeePaymentDetailByPaymentId(payStudentFeePayment.getId());
                for (PayStudentFeePaymentDetail payStudentFeePaymentDetail : payStudentFeePaymentDetailList) {
                    StudentPayVos studentPayVos = new StudentPayVos();
                    studentPayVos.setMoney(payStudentFeePaymentDetail.getTotalAmount());
                    studentPayVos.setStudentId(studentId);
                    studentPayVos.setCreateTime(new Date());
                    studentPayVos.setUpdateTime(new Date());
                    studentPayVos.setSelfMoney(payStudentFeePaymentDetail.getPaidAmount());
                    studentPayVos.setFeeTime(new Date());
                    if (payStudentFeePaymentDetail.getPayStatus() == 20 || payStudentFeePaymentDetail.getPayStatus() == 30) {
                        studentPayVos.setPayStatus(1L);
                        studentPayVos.setStatus(0L);
                        studentPayVos.setPaymentMethod(String.valueOf(payStudentFeePaymentDetail.getPayMethod()));
                    }
                    if (payStudentFeePaymentDetail.getPayStatus() == 50){
                        studentPayVos.setPayStatus(0L);
                        studentPayVos.setStatus(1L);
                        studentPayVos.setRefundMethod("已退款");
                    }
                    if (payStudentFeePaymentDetail.getPayStatus() == 10){
                        studentPayVos.setPayStatus(0L);
                        studentPayVos.setPaymentMethod(String.valueOf(payStudentFeePaymentDetail.getPayMethod()));
                    }
                    if (payStudentFeePaymentDetail.getPayStatus() == 40){
                        studentPayVos.setPayStatus(2L);
                        studentPayVos.setStatus(0L);
                        studentPayVos.setPaymentMethod(String.valueOf(payStudentFeePaymentDetail.getPayMethod()));
                    }
                    studentPayVos.setMoneyType(payStudentFeePayment.getFeeYear());
                    studentPayVos.setTitle(payStudentFeePaymentDetail.getFeeItemConfigName());
                    studentPayVos.setBeforePay(payStudentFeePaymentDetail.getPrepaymentDeducted());
                    studentPayVos.setFeeYearId(Math.toIntExact(payStudentFeePayment.getFeeYearId()));
                    studentPayVos.setFeeYearName(payStudentFeePayment.getFeeYear());
                    studentPayVosList.add(studentPayVos);

                }
            }
        }
        return studentPayVosList;
    }

    @Override
    public Map<String,Object> updateAllPayStudentFeePayment(String payNumber, Long status) {
        //修改大订单状态
        PayStudentFeePayment payStudentFeePayment = new PayStudentFeePayment();
        payStudentFeePayment.setPayNo(payNumber);
        payStudentFeePayment.setPayStatus(status);
        payStudentFeePayment.setUpdateTime(new Date());
        payStudentFeePaymentMapper.updatePayStudentFeePayment1(payStudentFeePayment);
        PayStudentFeePaymentDetail payStudentFeePaymentDetail = new PayStudentFeePaymentDetail();
        payStudentFeePaymentDetail.setPayNo(payNumber);
        payStudentFeePaymentDetail.setPayStatus(Math.toIntExact(status));
        payStudentFeePaymentDetailMapper.updatePayStudentFeePaymentDetail1(payStudentFeePaymentDetail);


        PayStudentFeePayment payStudentFeePayment1 = new PayStudentFeePayment();
        payStudentFeePayment1.setPayNo(payNumber);
        List<PayStudentFeePayment> payStudentFeePaymentList = payStudentFeePaymentMapper.selectPayStudentFeePaymentList(payStudentFeePayment1);



        PayStudentFee payStudentFee = new PayStudentFee();
        payStudentFee.setBillNo(payStudentFeePaymentList.get(0).getBillNo());
        payStudentFee.setBillStatus(Math.toIntExact(status));
        payStudentFee.setUpdateTime(new Date());
        payStudentFee.setLastPaymentTime(new Date());
        payStudentFeeMapper.updatePayStudentFee1(payStudentFee);

        PayStudentFeeItem payStudentFeeItem = new PayStudentFeeItem();
        payStudentFeeItem.setBillNo(payStudentFeePaymentList.get(0).getBillNo());
        payStudentFeeItem.setPayStatus(Math.toIntExact(status));
        payStudentFeeItem.setUpdateTime(new Date());
        payStudentFeeItemMapper.update1(payStudentFeeItem);

        Map<String,Object> map = new HashMap<>();
        map.put("studentId",payStudentFeePaymentList.get(0).getStudentId());
        map.put("feeYearId",payStudentFeePaymentList.get(0).getFeeYearId());
        return map;
    }

    @Override
    public PayStudentFeePaymentVos updateStudentFeePayment(PayStudentFeePaymentVos payStudentFeePaymentVos) {

        payStudentFeePaymentMapper.updatePayStudentFeePaymentByPayNo(payStudentFeePaymentVos);
        PayStudentFeePaymentDetail payStudentFeePaymentDetail = new PayStudentFeePaymentDetail();
        payStudentFeePaymentDetail.setPayStatus(Math.toIntExact(payStudentFeePaymentVos.getPayStatus()));
        payStudentFeePaymentDetail.setPayNo(payStudentFeePaymentVos.getPayNo());
        payStudentFeePaymentDetailMapper.updatePayStudentFeePaymentDetailByPayNo(payStudentFeePaymentDetail);

        PayStudentFeePaymentVos payStudentFeePaymentVo = payStudentFeePaymentMapper.selectByPayNo(payStudentFeePaymentVos.getPayNo());
        List<PayStudentFeePaymentDetail> payStudentFeePaymentDetailList = payStudentFeePaymentDetailMapper.selectPayStudentFeePaymentDetailByPayNo(payStudentFeePaymentVos.getPayNo());
        //支付失败才修改状态
        if (payStudentFeePaymentVos.getPayStatus() == 40){
            PayStudentFee payStudentFee = new PayStudentFee();
            payStudentFee.setId(Math.toIntExact(payStudentFeePaymentVo.getStudentFeeId()));
            payStudentFee.setBillStatus(10);
            payStudentFee.setUpdateTime(new Date());
            payStudentFeeMapper.updatePayStudentFee(payStudentFee);
        }

        for (PayStudentFeePaymentDetail payStudentFeePaymentDetail1 : payStudentFeePaymentDetailList) {
            PayStudentFeeItem payStudentFeeItem = new PayStudentFeeItem();
            payStudentFeeItem.setId(payStudentFeePaymentDetail1.getStudentFeeItemId());
            payStudentFeeItem.setPayStatus(Math.toIntExact(payStudentFeePaymentVos.getPayStatus()));
            payStudentFeeItem.setUpdateTime(new Date());
            payStudentFeeItemMapper.updateDetailPay(payStudentFeeItem);
        }
        return payStudentFeePaymentVo;
    }
}
